package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingPerformance;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingPerformanceRepository;

/**
 * 老师 ---备授课--授课报告-课堂表现
 * 
 */
@CrossOrigin
@RestController
public class LectureReportClassPerformanceController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingPerformanceRepository yxyEsbagTeachingPerformanceRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/performance/class/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String id = reqModel.id;
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String classId = reqModel.classId;
        Assert.notNull(StringUtils.defaultIfBlank(id, null), "授课包ID为空");
        RespModel respModel = new RespModel();
        // 表现正常 0： Performance
        List<ResModel> normalList = new ArrayList<ResModel>();
        List<YxyEsbagTeachingPerformance> yPerformances = yxyEsbagTeachingPerformanceRepository
                .findByTeacherIdAndSchoolIdAndPackIdAndPerformanceAndClassId(userId, schoolId, id, 0, classId);
        String packName = "";
        Long userTime = 0l;
        for (YxyEsbagTeachingPerformance yxyEsbagTeachingPerformance : yPerformances) {
            ResModel model = new ResModel();
            model.classId = yxyEsbagTeachingPerformance.getClassId();
            model.packId = yxyEsbagTeachingPerformance.getPackId();
            model.avantar = yxyEsbagTeachingPerformance.getAvantar() == null ? "null"
                    : yxyEsbagTeachingPerformance.getAvantar();
            model.studentNick = yxyEsbagTeachingPerformance.getStudentNick();
            packName = yxyEsbagTeachingPerformance.getPackName();
            userTime = yxyEsbagTeachingPerformance.getUserTime();
            model.studentId = yxyEsbagTeachingPerformance.getStudentId();
            model.normal = yPerformances.size();
            normalList.add(model);
        }

        respModel.normalList = normalList;
        // 表现-表扬 1： Performance
        List<ResModel> praiseList = new ArrayList<ResModel>();
        List<YxyEsbagTeachingPerformance> yPerformances1 = yxyEsbagTeachingPerformanceRepository
                .findByTeacherIdAndSchoolIdAndPackIdAndPerformanceAndClassId(userId, schoolId, id, 1, classId);
        for (YxyEsbagTeachingPerformance yxyEsbagTeachingPerformance : yPerformances1) {
            ResModel model = new ResModel();
            if (null != userTime) {
                if (StringUtils.isBlank(packName) || 0 == userTime) {
                    packName = yxyEsbagTeachingPerformance.getPackName();
                    userTime = yxyEsbagTeachingPerformance.getUserTime();
                }
            }

            model.classId = yxyEsbagTeachingPerformance.getClassId();
            model.packId = yxyEsbagTeachingPerformance.getPackId();
            model.avantar = yxyEsbagTeachingPerformance.getAvantar() == null ? "null"
                    : yxyEsbagTeachingPerformance.getAvantar();
            model.studentNick = yxyEsbagTeachingPerformance.getStudentNick();
            model.studentId = yxyEsbagTeachingPerformance.getStudentId();
            model.praise = yPerformances1.size();
            praiseList.add(model);
        }
        respModel.praiseList = praiseList;
        // 表现-批评 2： Performance
        List<ResModel> criticizeList = new ArrayList<ResModel>();
        List<YxyEsbagTeachingPerformance> yPerformances2 = yxyEsbagTeachingPerformanceRepository
                .findByTeacherIdAndSchoolIdAndPackIdAndPerformanceAndClassId(userId, schoolId, id, 2, classId);
        for (YxyEsbagTeachingPerformance yxyEsbagTeachingPerformance : yPerformances2) {
            ResModel model = new ResModel();
            if (null != userTime) {
                if (StringUtils.isBlank(packName) || 0 == userTime) {
                    packName = yxyEsbagTeachingPerformance.getPackName();
                    userTime = yxyEsbagTeachingPerformance.getUserTime();
                }
            }
            model.classId = yxyEsbagTeachingPerformance.getClassId();
            model.packId = yxyEsbagTeachingPerformance.getPackId();
            model.studentId = yxyEsbagTeachingPerformance.getStudentId();
            model.avantar = yxyEsbagTeachingPerformance.getAvantar() == null ? "null"
                    : yxyEsbagTeachingPerformance.getAvantar();
            model.studentNick = yxyEsbagTeachingPerformance.getStudentNick();
            model.criticeze = yPerformances2.size();
            criticizeList.add(model);
        }
        respModel.criticizeList = criticizeList;
        // 表现-缺勤 3： Performance
        List<ResModel> absenceList = new ArrayList<ResModel>();
        List<YxyEsbagTeachingPerformance> yPerformances3 = yxyEsbagTeachingPerformanceRepository
                .findByTeacherIdAndSchoolIdAndPackIdAndPerformanceAndClassId(userId, schoolId, id, 3, classId);
        for (YxyEsbagTeachingPerformance yxyEsbagTeachingPerformance : yPerformances3) {
            ResModel model = new ResModel();
            if (null != userTime) {
                if (StringUtils.isBlank(packName) || 0 == userTime) {
                    packName = yxyEsbagTeachingPerformance.getPackName();
                    userTime = yxyEsbagTeachingPerformance.getUserTime();
                }
            }
            model.classId = yxyEsbagTeachingPerformance.getClassId();
            model.packId = yxyEsbagTeachingPerformance.getPackId();
            model.avantar = yxyEsbagTeachingPerformance.getAvantar() == null ? "null"
                    : yxyEsbagTeachingPerformance.getAvantar();
            model.studentId = yxyEsbagTeachingPerformance.getStudentId();
            model.studentNick = yxyEsbagTeachingPerformance.getStudentNick();
            model.absence = yPerformances3.size();
            absenceList.add(model);
        }
        respModel.absenceList = absenceList;
        respModel.packName = packName;
        respModel.userTime = userTime;
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 授课包id
        public String id;
        // 授课班级ID
        public String classId;
    }

    protected class ResModel {
        // 授课包id
        public String packId;
        // 授课班级ID
        public String classId;
        // 正常人数
        public Integer normal;
        // 表扬人数
        public Integer praise;
        // 批评人数
        public Integer criticeze;
        // 缺勤人数
        public Integer absence;
        // 学生头像
        public String avantar;
        // 学生名称
        public String studentNick;
        public String studentId;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 课堂表现-正常
        public List<ResModel> normalList;
        // 课堂表现-表扬
        public List<ResModel> praiseList;
        // 课堂表现-批评
        public List<ResModel> criticizeList;
        // 课堂表现-缺勤
        public List<ResModel> absenceList;
        // 授课包名称
        public String packName;
        // 授课时长
        public Long userTime;

    }
}
